<!DOCTYPE HTML>
<html>
<head>
    <meta name="viewport" content="width=device-width user-scalable= 'no'">
    {include file="common:meta"}
    <style>
        div.footer div.pull-right{
            display: none !important;
        }
        body{
            padding-top: constant(safe-area-inset-top);
            padding-left: constant(safe-area-inset-left);
            padding-right: constant(safe-area-inset-right);
            padding-bottom: constant(safe-area-inset-bottom);
        }
        select{

            appearance:none  !important;
            border:1px solid darkgrey !important;
            font-size:18px !important;
            padding:0px 10px !important;
            display:block !important;
            width:100%;
            -webkit-box-sizing:border-box;
            box-sizing: border-box;
            background-color: #FFFFFF;
            color:#333333;
            border-radius:4px !important;
        }
    </style>
</head>
<script src="https://cdn.bootcss.com/fastclick/1.0.6/fastclick.min.js"></script>
<body>
<div class="page-container">
    <div class=" float-e-margins">
        <div class="ibox-content">
            <div class="tabs-container">
                <ul class="nav nav-tabs">
                    <li class=""><a data-toggle="tab" href="#tab-3" aria-expanded="true">审批</a>
                    </li>
                    <li class="active"><a data-toggle="tab" href="#tab-1" aria-expanded="true">领取列表</a>
                    </li>
                    <li class=""><a data-toggle="tab" href="#tab-2" aria-expanded="true">官方赠送</a>
                    </li>

                </ul>
                <div class="tab-content">
                    <div class="tab-pane" id="tab-3">

                        <table id="tb_departments3"></table>

                    </div>
                    <div class=" tab-pane active" id="tab-1">
                        <form action="" class="form-inline" data-url="">
                            <div class="form-group">
                                <label class="control-label" for="key">选项:</label>
                                <select id="key" class="form-control" name="key">
                                    <option value="u_id">ID</option>
                                    <option value="account">赠送类型</option>
                                    <option value="admin">管理员</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label class="control-label" for="value">输入:</label>
                                <input class="form-control" id="value" type="text" name="value">
                            </div>
                            <button type="button" class="btn btn-primary" id="eventQuery">查询</button>
                            <button type="button" onclick="location.replace(location.href);" class="btn btn-success ">刷新</button>
                        </form>
                        <table id="tb_departments"></table>
                    </div>
                    <div class="tab-pane" id="tab-2">
                        <div class="ibox-content">

                            <form class="form-horizontal"  id="form"   data-url="/GivePresenAdd" enctype="multipart/form-data">
                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="user_id">
                                        <span class="c-red">*</span>ID：
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <input type="text" class="form-control" id="user_id" value="" name="user_id">
                                    </div>
                                </div>
                                <div id="gf" class="hidden">
                                    <div class="form-group">
                                        <label class="col-sm-2 control-label" for="a1">
                                            <span class="c-red">*</span>奖励类型：
                                        </label>
                                        <div class="col-md-2 col-sm-10">
                                            <select class="form-control" name="a1" id="a1">
                                                <option value="1">金币</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-2 control-label" for="a2">
                                            <span  class="c-red">*奖励数量：</span>
                                        </label>
                                        <div class="col-md-2 col-sm-10">
                                            <input type="number" class="form-control" id="a2" value="" name="a2">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-2 control-label" for="a3">
                                            <span class="c-red">*</span>奖励类型：
                                        </label>
                                        <div class="col-md-2 col-sm-10">
                                            <select class="form-control" name="a3" id="a3">
                                                <option value="2">钻石</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-2 control-label" for="a4">
                                            <span id="custom_two" class="c-red">*奖励数量：</span>
                                        </label>
                                        <div class="col-md-2 col-sm-10">
                                            <input type="number" class="form-control" id="a4" value="" name="a4">
                                        </div>
                                    </div>
                                </div>
                                <div id="where_area">
                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="prize_type">
                                        <span class="c-red">*</span>奖励类型：
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <select class="form-control" name="prize_type" id="prize_type">
                                            <option value="1">金币</option>
                                            <option value="2">钻石</option>
                                            <option value="3">座驾</option>
                                            <option value="4">礼物</option>
                                            <option value="5">喇叭</option>
                                            <option value="6">生肖卡</option>
                                        </select>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="prize_id">
                                        <span class="c-red">*</span>奖励奖品：
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <select class="form-control" name="prize_id" id="prize_id">
                                        </select>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="prize_number">
                                        <span id="custom" class="c-red">*奖励数量：</span>
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <input type="number" class="form-control" id="prize_number" value="" name="prize_number">
                                    </div>
                                </div>
                                </div>
                                <div class="form-group" data-tap-disabled="true">
                                    <label class="col-sm-2 control-label" for="account">
                                        <span class="c-red">*赠送类型：</span>
                                    </label>
                                    <div class="col-md-2 col-sm-10" data-tap-disabled="true">

                                        <select class="form-control" name="account" id="account">
                                            <option value="活动赠送">活动赠送</option>
                                            <option value="活跃市场">活跃市场</option>
                                            <option value="官方后台冲值">官方后台冲值</option>
                                            <option value="官方账户发放奖品预存">官方账户发放奖品预存</option>
                                            <option value="其他">其他</option>
                                            <option value="球币申请">球币申请</option>
                                        </select>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="content">
                                        <span class="c-red">*原因必填：</span>
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <textarea name="content" id="content"  cols="30" rows="7">
                                        </textarea>
                                    </div>
                                </div>


                                <div class="form-group" id="form2">
                                    <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">
                                        <button id="btn2" class="btn btn-primary" type="button" onclick="commonSubmit(this)">提交
                                        </button>
                                        <button id="cp_btn2" class="btn btn-primary hidden" type="button">申请
                                        </button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>
{include file="common:footer"}

<script>

    $(function() {
        FastClick.attach(document.body);
        $('#account').on('change',function (e) {
            var val = $(this).val();
           
            if(val == '球币申请'){
                $('#gf').removeClass('hidden');
                $('#where_area').addClass('hidden');
                $('#btn2').addClass('hidden');
                $('#cp_btn2').removeClass('hidden');
            }else{
                $('#gf').addClass('hidden');
                $('#where_area').removeClass('hidden');
                $('#btn2').removeClass('hidden');
                $('#cp_btn2').addClass('hidden');
            }
        });

        initTable();
        initTable3();

        $("#eventQuery").click(function () {
            $('#tb_departments').bootstrapTable(('refresh'));	// 很重要的一步，刷新url！
        });
        $("#cp_btn2").on("click",function(){
            var that = this;


            var finalRes = $("#form").serializeArray().reduce(function(result, item){
                result[item.name] = item.value;
                return result;
            }, {});
            if(finalRes['a2'] === "" && finalRes['a4'] === ""){
                    layer.msg('金币数和钻石数不能同时为空', { icon: 2, time: 2000});
                    return false;
            }
            if(isNaN(Number(finalRes['a2'])) || isNaN(Number(finalRes['a4']))){
                layer.msg('输入不合法,金币数和钻石数必须为数字', { icon: 2, time: 2000});
                return false;
            }

            if((Number(finalRes['a2']) + Number(finalRes['a4']) <= 0) || (Number(finalRes['a2']) < 0 && Number(finalRes['a4']) > 0) || (Number(finalRes['a2']) > 0 && Number(finalRes['a4']) < 0)){
                layer.msg('金币数或者钻石数必须有一个要大于0，并且不能有小于0的数量', { icon: 2, time: 2000});
                return false;
            }
            $(that).removeAttr('disabled').removeClass('disabled');
            $('#form input').val('');
            $('#form textarea').val('');
            $.ajax({
                url: '/GivePresenLargess',
                type: 'POST',
                dataType: 'json',
                data:JSON.stringify(finalRes),
                processData: false,
                contentType: "application/json",
                cache: false,
                async: false,
                beforeSend: function(){
                    $(that).attr("disabled","disabled").addClass('disabled');
                },
                success: function (re) {


                    if (re.code === 100) {
                        layer.msg(re.msg, {
                            icon: 1,
                            time: 2000 //2秒关闭（如果不配置，默认是3秒
                        }, function () {
                            $(that).removeAttr('disabled').removeClass('disabled');
                            // re.url ? window.location.href = re.url : '';
                        });
                        //  window.location.href=document.referrer;
                    } else {
                        layer.msg(re.msg,{ icon: 2,time: 2000});
                        $(that).removeAttr('disabled').removeClass('disabled');
                    }
                },
                error: function () {
                    $(that).removeAttr('disabled').removeClass('disabled');
                },
                complete: function () {
                    $(that).removeAttr('disabled').removeClass('disabled');
                }
            });
        });
    });

    function initTable() {
        $('#tb_departments').bootstrapTable({
            url: '/GivePresenIndexData',         //请求后台的URL（*）
            dataField: "data",
            method: 'get',                      //请求方式（*）
            striped: true,                      //是否显示行间隔色
            cache: true,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
            pagination: true,                   //是否显示分页（*）
            queryParams: queryParams,//传递参数（*）
            showToggle: true,
            showRefresh: true,
            sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
            pageNumber: 1,                       //初始化加载第一页，默认第一页
            pageSize: 10,                       //每页的记录行数（*）
            pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
            showColumns: true,                  //是否显示所有的列
            showPaginationSwitch: true,
            minimumCountColumns: 2,             //最少允许的列数

            columns: [{
                field: 'u_id',
                title: 'ID'
            }, {
                field: 'nickname',
                title: '用户昵称'
            }, {
                field: 'prize_type',
                title: '奖品类型'
            },{
                field: 'gift_name',
                title: '奖品名称'
            }, {
                field: 'prize_number',
                title: '奖品数量(或天数)'
            },  {
                field: 'account',
                title: '赠送类型'
            },{
                field: 'content',
                title: '赠送原因'
            },{
                field: 'remark',
                title: '备注'
            }, {
                field: 'create_time',
                title: '申请时间',
                sortable: true
            }, {
                field: 'approv_time',
                title: '审批时间',
                sortable: true,
                formatter: function (value, row, index) {
                    // var str = parseInt(value) * 1000;//将php时间戳转化为整形并乘以1000
                    // var newDate = new Date(str);
                    // var year = newDate.getUTCFullYear();//取年份
                    // var month = newDate.getUTCMonth() + 1;//取月份
                    // var nowday = newDate.getUTCDate();//取天数
                    // var hours = newDate.getHours();//取小时
                    // var minutes = newDate.getMinutes();//取分钟
                    // var seconds = newDate.getSeconds();//取秒
                    // return year + "-" + month + "-" + nowday + " " + hours + ":" + minutes + ":" + seconds;
                    var str = Number(value) * 1000;
                    var g = new Date(str);
                    var year=g.getFullYear();  //取得4位数的年份
                    var month=g.getMonth()+1;  //取得日期中的月份，其中0表示1月，11表示12月
                    var date=g.getDate();      //返回日期月份中的天数（1到31）
                    var hour=g.getHours();     //返回日期中的小时数（0到23）
                    var minute=g.getMinutes(); //返回日期中的分钟数（0到59）
                    var second=g.getSeconds(); //返回日期中的秒数（0到59）
                    return (year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second) == '1970-1-1 8:0:0' ? '': year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;


                }
            }
            , {
                field: 'status',
                title: '状态',
                align: 'center',
                formatter: function (value, row, index) {

                    if(row.status == 0 ){
                        return "<b style='color: #ec971f;'>审核中</b>";
                    }else if(row.status == 1){
                        return "<b style='color: #108733;'>审核通过</b>";
                    }else{
                        return "<b style='color: red'>审核失败</b>";
                    }
                }
            }]
        });
    }
    function initTable3() {
        $('#tb_departments3').bootstrapTable({
            url: '/GivePresenApproveData',         //请求后台的URL（*）
            dataField: "data",
            method: 'get',                      //请求方式（*）
            striped: true,                      //是否显示行间隔色
            cache: true,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
            pagination: true,                   //是否显示分页（*）
            queryParams: queryParams,//传递参数（*）
            showToggle: true,
            showRefresh: true,
            sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
            pageNumber: 1,                       //初始化加载第一页，默认第一页
            pageSize: 10,                       //每页的记录行数（*）
            pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
            showColumns: true,                  //是否显示所有的列
            showPaginationSwitch: true,
            minimumCountColumns: 2,             //最少允许的列数

            columns: [{
                field: 'u_id',
                title: 'ID'
            }, {
                field: 'nickname',
                title: '用户昵称'
            },{
                field: 'content',
                title: '赠送原因'
            }, {
                field: 'create_time',
                title: '发放时间',
                sortable: true
            },{
                field: 'Button',
                title: '操作',
                events: operateEvents,
                formatter: function (value, row, index) {
                    var id = row.id;
                    return [
                        '<button type="button" data-bid="'+id+'" id="reset-password" class="btn-primary build-union btn " style="margin-right:15px;">审批</button>',

                    ];
                }
            }]
        });
    }

    // window.operateEvents = {
    // };

    //得到查询的参数
    function queryParams(params) {
        params.offset = params.offset / params.limit + 1;
        params.key = $('#key').val();
        params.value = $('#value').val();
        console.log(JSON.stringify(params));
        return params;
    }

    function operateFormatter(value, row, index) {
    }
    // 索按钮触发事件

    $('#prize_type').change(function(){
        var prize_type = $(this).val();
        if(prize_type == 3)
            $('#custom').html('*奖励天数：');
        else
            $('#custom').html('*奖励数量：');
        console.log($('#custom').text());

        $.ajax({
            type: 'get',
            dataType: 'json',
            url: '/GivePresenGiftName',
            data: {'prize_type': prize_type},
            success: function (re) {
                if (re.code === 100) {

                    console.log(re);
                    var htmlText = "<option value='' selected>--请选择--</option>";
                    if(re.data !== null){

                            for(var i in re.data){

                                if(prize_type == 4)
                                    htmlText+="<option value="+ re.data[i]['id']+">"+ re.data[i]['name']+'--'+re.data[i]['diamonds']+"</option>";
                                else
                                    htmlText+="<option value="+ re.data[i]['id']+">"+ re.data[i]['name']+"</option>";
                            }

                        $('#prize_id').html(htmlText);
                    }
                }else{
                    layer.msg(re.msg,{
                        icon:2,
                        time:1500
                    });
                }
            },
            error: function (data) {
                layer.msg('服务器出小差了',{
                    icon:2,
                    time:1500
                });
            }
        });
    });

    $('#key').change(function(){

        if($(this).val() === 'account'){
            var html = '<select id="value" class="form-control" name="value">' +
                '<option value="活动赠送">活动赠送</option>' +
                '<option value="活跃市场">活跃市场</option>' +
                '<option value="官方后台冲值">官方后台冲值</option>' +
                '<option value="官方账户发放奖品预存">官方账户发放奖品预存</option>' +
                '<option value="其他">其他</option>' +
                '<option value="球币申请">球币申请</option>' +
                '</select>';
            $('#value').replaceWith(html);
        }else
            $('#value').replaceWith('<input class="form-control" id="value" type="text" name="value">');

    });
    window.operateEvents = {

        'click .build-union':function(e,value,row,index){
            var that = this;

            layer.open({
                type: 2,
                title: ['审批','font-size:18px;font-family:宋体;font-weight:700;'],
                shadeClose: false,
                shade: true,
                maxmin: false, //开启最大化最小化按钮
                area: ['100%', '100%'],
                content:"/GivePresenApproveView?id=" + that.dataset.bid,  //type=1 批量禁封 不是则单个禁封
                offset:'auto',
                scrollbar:false,
                end: function () {

                    // $('#tb_departments').bootstrapTable(('refresh'));	// 很重要的一步，刷新url！
                }
            });

        }

    };

</script>
</body>
</html>